package com.jaya.budan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.GeRen_Main_Fans_ListView_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class Set_Hide_BlackList_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private TextView tv_set_hide_clear;
    private String access_token;
    private MyListView main_listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_hide_black_list);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        tv_set_hide_clear = findViewById(R.id.tv_set_hide_clear);
        main_listview = findViewById(R.id.main_listview);
        back.setOnClickListener(this);
        tv_set_hide_clear.setOnClickListener(this);
        postData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_set_hide_clear:
                JSONObject object = new JSONObject();
                try {
                    object.put("uid", "");//拉黑用户ID
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postDeleteData(object);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.tr_user_information_black_list_Url + access_token, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        List<User_Limit> user_limit = gson.fromJson(info.toString(), new TypeToken<List<User_Limit>>(){}.getType());
                        main_listview.setAdapter(new GeRen_Main_Fans_ListView_Adapter(Set_Hide_BlackList_Activity.this, user_limit, 1, 0));
                    } else {
                        ToolUtils.showToast(Set_Hide_BlackList_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Set_Hide_BlackList_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postDeleteData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.tr_user_information_delete_black_user_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                    } else {
                        ToolUtils.showToast(Set_Hide_BlackList_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Set_Hide_BlackList_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
